<template>
	<view>
		<view class="foot_fix">
			<view class="foot_nav">
				<view class="foot_item" v-for="(item,index) in navbarList" :key="index" @click="urlChange(index)">
					<block v-if="item.type">
						<!-- <view class="chatags" v-if="item.title=='聊天'&&unread==1">...</view> -->
						<!-- <view class="chatags" v-if="item.title=='聊天'">{{unread}}</view> -->
						<image :src="currentIndex == index?item.selectpath:item.iconpath" mode="heightFix"></image>
						<view :class="currentIndex == index?'foot_titleon foot_title':'foot_title'">{{item.title}}
						</view>
					</block>
					<block v-else>
						<view :class="currentIndex == 1?'foot_circles':'foot_circle'" @click="submitChange">
							<view :class="submitStatus.tranStatus?'submit_trans rotate45':'submit_trans'">
								<u-icon name="plus" color="#fff" size="42"></u-icon>
							</view>
						</view>
					</block>
				</view>
			</view>
		</view>
		<view class="mask" v-if="popupSubmit" @click="submitChange">
			<view class="popup_Subwindow action_height">
				<block v-for="(item,index) in submitList" :key="index">
					<navigator :url="item.url" hover-class="none"
						:class="index==0?'popup_subitem popup_subitemJ':index==1?'popup_subitem popup_subitemO':index==2?'popup_subitem popup_subitemK':''">
						<image :src="item.icon" mode="aspectFill"></image>
						<text>{{item.title}}</text>
					</navigator>
				</block>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			currentIndex: {
				type: Number,
				default: 1,
				showService: true,
			},
			// unread: ""
		},
		data() {
			return {
				// unread: wx.getStorageSync('unread'),
				navbarList: [{
					title: '首页',
					type: 1,
					iconpath: '/static/img/index.png',
					selectpath: '/static/img/indexL.png',
					url: '/pages/index/index',
					tag: 0
				}, {
					title: '社区',
					type: 1,
					iconpath: '/static/img/sq.png',
					selectpath: '/static/img/sqL.png',
					tag: 1,
					url: '/pages/community/pagetwo?isactive=0',
					// url: '/pages/community/index?isactive=1',
				}, {
					title: '发布',
					type: 0,
					tag: 2
				}, {
					type: 1,
					title: '聊天',
					iconpath: '/static/img/news.png',
					selectpath: '/static/img/newsL.png',
					url: '/pages/chathome/chathome',
					tag: 3
				}, {
					title: '我的',
					type: 1,
					iconpath: '/static/img/my.png',
					selectpath: '/static/img/myL.png',
					url: '/pages/personal/personal',
					tag: 4
				}], // 导航列表
				submitStatus: {
					tranStatus: false
				}, //发布按钮状态
				popupSubmit: false, //发布弹窗状态
				submitList: [{
					title: '发布供应',
					icon: '/static/img/sub1.png',
					url: '/home/submit/submitsupply'
				}, {
					title: '发布求购',
					icon: '/static/img/sub2.png',
					url: '/home/submit/submitwanted'
				}, {
					title: '发布动态',
					icon: '/static/img/sub3.png',
					url: '/home/submit/submit'
				}], //发布弹窗按钮

				list: [],
				msg: {}, //发送过来的消息

				msglist: '',

				cList: [],
				from_id: wx.getStorageSync('client_uid'),

				unread: "",
			}
		},
		mounted() {
			// console.log(11111)
			let _this = this

			// wx.connectSocket({
			// 	url: _this.$api.connectUrl(),
			// 	header: {
			// 		'content-type': 'application/json'
			// 	},
			// 	method: "get",
			// 	success: (data) => {
			// 		console.log(data)
			// 		//如果链接成功，则监听 WebSocket 连接打开事件
			// 		if (data.errMsg == "connectSocket:ok") {
			// 			//监听 WebSocket 连接打开事件
			// 			this.handleSocketOpen();
			// 		}
			// 	}
			// })

			// this.handleSocketMessage();
		},

		methods: {
			submitChange: function() {
				let _this = this
				_this.$data.submitStatus.tranStatus = !_this.$data.submitStatus.tranStatus
				if (_this.$data.submitStatus.tranStatus) {
					_this.popupSubmit = true
				} else {
					_this.popupSubmit = false
				}
				_this.$emit('lai', _this.popupSubmit)

			},
			urlChange: function(_index) {
				if (this.$data.navbarList[_index].type) {
					if (this.$data.navbarList[_index].tag != this.currentIndex) {
						uni.redirectTo({
							url: this.$data.navbarList[_index].url
						})
					}
				}
			},

			//监听 WebSocket 连接打开事件
			handleSocketOpen() {
				wx.onSocketOpen(() => {
					//定义状态值，如果为true则表示监听成功
					setTimeout(function() {
						wx.sendSocketMessage({
							data: "ping",
						})
					}, 3000)
				})
			},

			handleSocketMessage() {
				console.log('接收消息')
				let _this = this

				wx.onSocketMessage((data) => {
					// console.log(JSON.parse(data.data))

					let info = {
						type: 'login',
						unique_id: '',
						formid: wx.getStorageSync('client_uid'),
					}
					let i, k
					i = JSON.parse(data.data)


					if (JSON.parse(data.data).type == 'init') {
						wx.sendSocketMessage({
							data: JSON.stringify(info),
							success: function() {
								console.log('发送成功')
							},
							fail: function() {
								console.log('发送失败')
							}
						})
					} else if (JSON.parse(data.data).type == 'unauthorized') {
						uni.showToast({
							title: '您还没有登录,请登录',
							icon: 'none',
							duration: 2000,
							success: function() {
								setTimeout(function() {
									uni.reLaunch({
										url: '/home/login/login'
									})
								}, 1000);
							}
						})
					} else if (JSON.parse(data.data).type == 'chatList') {
						_this.list = JSON.parse(i.content)

						let arr = []
						arr = _this.list

						//设置导航栏未读消息
						let unread
						arr.some((item) => {
							return unread = 1
						})
						_this.list = arr
						console.log(_this.list, '_this.list')
					} else if (JSON.parse(data.data).type == 'chatMessage') {
						i.formid = Number(i.formid)

						if (_this.list.length == 0) {
							uni.reLaunch({
								url: '/pages/chathome/chathome'
							})
						} else {
							let arr = []
							arr = _this.list
							//设置导航栏未读消息
							// let unread
							// arr.some((item) => {
							// 	return unread = 1
							// })

							_this.list = arr
						}
						console.log(_this.list, 'chatMessage')

					}

				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	.foot_fix {
		height: 98rpx;
		height: calc(env(safe-area-inset-bottom) + 98rpx);
		position: fixed;
		bottom: 0;
		width: 100%;
		background: #fff;
		z-index: 10002;
		box-shadow: 0 0 43rpx 0 rgba(226, 226, 226, 0.44);

		.foot_nav {
			width: 100%;
			height: 98rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-around;
			z-index: 10002;

			.foot_item {
				width: 20%;
				align-items: center;
				justify-content: center;
				display: flex;
				flex-direction: column;
				position: relative;

				.chatags {
					width: 25rpx;
					height: 25rpx;
					border-radius: 50%;
					background-color: #FA3931;
					color: #fff;
					font-size: 24rpx;
					text-align: center;
					line-height: 10rpx;
					position: absolute;
					right: 20px;
					top: -5px;
				}

				.foot_title {
					font-size: 24rpx;
					color: #868686;
					margin-top: 4rpx;
				}

				.foot_titleon {
					color: #38be8c;
				}

				image {
					height: 52rpx;
					width: auto;
				}

				.foot_circle {
					width: 126rpx;
					height: 126rpx;
					background-color: #fff;
					border-radius: 50%;
					margin-top: -38rpx;
					display: flex;
					justify-content: center;
					align-items: center;
				}

				.submit_trans {
					width: 88rpx;
					height: 88rpx;
					border-radius: 50%;
					background: #38be8c;
					display: flex;
					justify-content: center;
					align-items: center;
					transition: 0.25s 0.1s;
				}

				.rotate45 {
					transform: rotate(45deg) scale(1.1);
				}

				.foot_circles {
					width: 126rpx;
					height: 126rpx;
					background-color: #fff;
					border-radius: 50%;
					display: flex;
					justify-content: center;
					align-items: center;
				}
			}
		}
	}

	.mask {
		width: 100%;
		height: 100vh;
		background: rgba($color: #000000, $alpha: 0.5);
		z-index: 10000;
		position: fixed;
		left: 0;
		top: 0;

		.popup_Subwindow {
			width: 100%;
			height: 450rpx;
			background-color: rgba($color: #fff, $alpha: 0.9);
			border-radius: 20rpx 20rpx 0 0;
			position: absolute;
			bottom: 98rpx;
			bottom: calc(env(safe-area-inset-bottom) + 98rpx);
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;

			.popup_subitem {
				display: flex;
				flex-direction: column;

				image {
					width: 120rpx;
					height: 120rpx;
				}

				text {
					margin-top: 12rpx;
					color: #222222;
					font-size: 28rpx;
				}
			}

			.popup_subitemJ {
				animation: moveJ 0.5s;
				animation-fill-mode: forwards;
				position: absolute;
				left: 120rpx;
				bottom: 110rpx;
				transition: 1s;

				@keyframes moveJ {
					from {
						bottom: 0;
						opacity: 0;
					}

					to {
						bottom: 110rpx;
						opacity: 1;
					}
				}
			}

			.popup_subitemO {
				animation: moveO 0.8s;
				animation-fill-mode: forwards;
				position: absolute;
				left: 319rpx;
				bottom: 245rpx;

				@keyframes moveO {
					from {
						bottom: 0;
						opacity: 0;
					}

					to {
						bottom: 215rpx;
						opacity: 1;
					}
				}
			}

			.popup_subitemK {
				animation: moveK 0.5s;
				animation-fill-mode: forwards;
				position: absolute;
				right: 120rpx;
				bottom: 110rpx;

				@keyframes moveK {
					from {
						bottom: 0;
						opacity: 0;
					}

					to {
						bottom: 110rpx;
						opacity: 1;
					}
				}
			}

		}
	}
</style>
